<?php
// To delete user from the database
if(isset($_POST['action']))
{
    $db = new Database();
    if($_POST['action']=="auditSearch")
    {
        $value = $_POST['value'];
        $textfield = "edi_textfield display"; // for textfield of edit.
        $selection = "editselection display"; // for selection of edit.
        $page = $_POST['page'];
        $cur_page = $page;
        $page -= 1;
        $per_page = 10; // Per page
        $previous_btn = true;
        $next_btn = true;
        $first_btn = true;
        $last_btn = true;
        $start = $page * $per_page;
	$action = $_POST['action'];
	//for sorting
	$markAudit = @$_POST['markSortAudit'];//for toggle of desc and asc 
	$fieldToSort = @$_POST['sortAuditRel'];//field to sort
	$sortfield = " ORDER BY at.datetime "; // default field to sort
	if(!empty($fieldToSort)){
	    $sortfield = " ORDER BY at.datetime ";
	}
	$sortType = " ASC ";
	//condition for sorting if desc or asc
	if($markAudit%2==0){
	    $sortType = " DESC ";
	}else{
	    $sortType = "ASC";
	}
	$sort =  $sortfield.$sortType; // concatinate sort
	//echo $sort;
        $finaldata.= "
        <table border='0' id='table_accomodation' class='table_accomodation'>
			    <thead>
				<tr>
				    <th class='sortAudit' rel='lastname'>User</th>
				    <th class='sortAudit' rel='action'>Action</th>
				    <th class='sortAudit' rel='datetime'>Date Time</th>
				</tr>
			    </thead>
			    <tbody id='searchUserTable'>";
        if(!empty($value))
        {
	    $queryString = "SELECT at.user_id as user_id, a.action as action,
				at.affected_table as affected_table, at.affected_record as affected_record,
				at.datetime as datetime, CONCAT(u.lastname,', ',u.firstname,' ',u.middlename) as name
				FROM tbaudit_trail at LEFT JOIN tbaction a on at.action_id=a.id
				LEFT JOIN tbuser u on u.id = at.user_id WHERE
				a.action LIKE '%$value%' or u.firstname LIKE '%$value%' or u.lastname LIKE '%$value%'
				or u.middlename LIKE '%$value%' and at.is_active=1";
            $query = $db->query($queryString." $sort LIMIT $start, $per_page", "array");
            $count = $db->query($queryString, "numrows");
            $no_of_paginations = ceil($count / $per_page);
        }
        else{
	    $queryString = "SELECT at.user_id as user_id, a.action as action,
				at.affected_table as affected_table, at.affected_record as affected_record,
				at.datetime as datetime, CONCAT(u.lastname,', ',u.firstname,' ',u.middlename) as name
				FROM tbaudit_trail at LEFT JOIN tbaction a on at.action_id=a.id
				RIGHT JOIN tbuser u on u.id = at.user_id WHERE at.is_active=1";
            $query = $db->query($queryString."$sort LIMIT $start, $per_page", "array");
            $count = $db->query($queryString, "numrows");
            $no_of_paginations = ceil($count / $per_page);
        }
        if($count>0){
        foreach($query as $data)
        {
        $finaldata.=
        '<tr id="deleteRow_'.$data['id'].'">
            <td>
                '.$data['name'].'
            </td>
            <td>
                '.$data['action'].'
            </td>
	    <td >
                '.$data['datetime'].'
            </td>
        </tr>';
        }
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$value);
        }else{
            $finaldata.="<tr><td colspan='5'><center>No Record Found</center></td></tr>";
            echo $finaldata;
        }
    }
}
